package com.nav.brainfuckeditor.editors;

import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.ITextDoubleClickStrategy;
import org.eclipse.jface.text.ITextHover;
import org.eclipse.jface.text.TextAttribute;
import org.eclipse.jface.text.presentation.IPresentationReconciler;
import org.eclipse.jface.text.presentation.PresentationReconciler;
import org.eclipse.jface.text.rules.DefaultDamagerRepairer;
import org.eclipse.jface.text.rules.Token;
import org.eclipse.jface.text.source.ISourceViewer;
import org.eclipse.jface.text.source.SourceViewerConfiguration;

public class BFConfiguration extends SourceViewerConfiguration {
	private BFDoubleClickStrategy doubleClickStrategy;
	private BFScanner scanner;
	private ColorManager colorManager;
	private BFTextHover textHover = new BFTextHover();

	public BFConfiguration(ColorManager colorManager) {
		this.colorManager = colorManager;
	}
	public String[] getConfiguredContentTypes(ISourceViewer sourceViewer) {
		return new String[] {
			IDocument.DEFAULT_CONTENT_TYPE,
			BFPartitionScanner.BF_DECREASE,
			BFPartitionScanner.BF_INCREASE,
			BFPartitionScanner.BF_INPUT,
			BFPartitionScanner.BF_OUTPUT,
			BFPartitionScanner.BF_LEFT,
			BFPartitionScanner.BF_RIGHT,
			BFPartitionScanner.BF_OPEN,
			BFPartitionScanner.BF_CLOSE};
	}
	public ITextHover getTextHover(ISourceViewer sourceViewer, String contentType, int stateMask) {
		return textHover;
	}
	public ITextHover getTextHover(ISourceViewer sourceViewer, String contentType) {
		return textHover;
	}
	public ITextDoubleClickStrategy getDoubleClickStrategy(
		ISourceViewer sourceViewer,
		String contentType) {
		if (doubleClickStrategy == null)
			doubleClickStrategy = new BFDoubleClickStrategy();
		return doubleClickStrategy;
	}

	protected BFScanner getBFScanner() {
		if (scanner == null) {
			scanner = new BFScanner(colorManager);
			scanner.setDefaultReturnToken(
				new Token(
					new TextAttribute(
						colorManager.getColor(IBFColorConstants.DEFAULT))));
		}
		return scanner;
	}

	public IPresentationReconciler getPresentationReconciler(ISourceViewer sourceViewer) {
		PresentationReconciler reconciler = new PresentationReconciler();

		DefaultDamagerRepairer dr = new DefaultDamagerRepairer(getBFScanner());
		reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE);
		reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE);

		NonRuleBasedDamagerRepairer ndr = new NonRuleBasedDamagerRepairer(new TextAttribute(colorManager.getColor(IBFColorConstants.BF_OPEN)));
		reconciler.setDamager(ndr, BFPartitionScanner.BF_OPEN);
		reconciler.setRepairer(ndr, BFPartitionScanner.BF_OPEN);

		ndr = new NonRuleBasedDamagerRepairer(new TextAttribute(colorManager.getColor(IBFColorConstants.BF_CLOSE)));
		reconciler.setDamager(ndr, BFPartitionScanner.BF_CLOSE);
		reconciler.setRepairer(ndr, BFPartitionScanner.BF_CLOSE);

		ndr = new NonRuleBasedDamagerRepairer(new TextAttribute(colorManager.getColor(IBFColorConstants.BF_INCREASE)));
		reconciler.setDamager(ndr, BFPartitionScanner.BF_INCREASE);
		reconciler.setRepairer(ndr, BFPartitionScanner.BF_INCREASE);

		ndr = new NonRuleBasedDamagerRepairer(new TextAttribute(colorManager.getColor(IBFColorConstants.BF_DECREASE)));
		reconciler.setDamager(ndr, BFPartitionScanner.BF_DECREASE);
		reconciler.setRepairer(ndr, BFPartitionScanner.BF_DECREASE);

		ndr = new NonRuleBasedDamagerRepairer(new TextAttribute(colorManager.getColor(IBFColorConstants.BF_INPUT)));
		reconciler.setDamager(ndr, BFPartitionScanner.BF_INPUT);
		reconciler.setRepairer(ndr, BFPartitionScanner.BF_INPUT);

		ndr = new NonRuleBasedDamagerRepairer(new TextAttribute(colorManager.getColor(IBFColorConstants.BF_OUTPUT)));
		reconciler.setDamager(ndr, BFPartitionScanner.BF_OUTPUT);
		reconciler.setRepairer(ndr, BFPartitionScanner.BF_OUTPUT);

		ndr = new NonRuleBasedDamagerRepairer(new TextAttribute(colorManager.getColor(IBFColorConstants.BF_LEFT)));
		reconciler.setDamager(ndr, BFPartitionScanner.BF_LEFT);
		reconciler.setRepairer(ndr, BFPartitionScanner.BF_LEFT);

		ndr = new NonRuleBasedDamagerRepairer(new TextAttribute(colorManager.getColor(IBFColorConstants.BF_RIGHT)));
		reconciler.setDamager(ndr, BFPartitionScanner.BF_RIGHT);
		reconciler.setRepairer(ndr, BFPartitionScanner.BF_RIGHT);

		return reconciler;
	}

}